package br.edu.ifrn.sgpapp.util;

import java.io.File;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;


public class ArquivoUtil {
	
	private static String DEFAULT_STORAGE_TYPE = Environment.DIRECTORY_DOWNLOADS;
	
	public static String getMimetype(String extensao){
		return android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extensao);
		
	}

	public static File getDefaultPath() {
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		}
		
		if(mExternalStorageAvailable && mExternalStorageWriteable) {
			File path = new File(Environment.getExternalStoragePublicDirectory(DEFAULT_STORAGE_TYPE),"");
			
			Log.d("getDefaultPath()", "Caminho = " + path.getAbsolutePath());

			return path;
		}
		
		return null;
	}
	
	public static Intent abrirArquivo(File file, String tipo){
		Intent intent = new Intent();
	    intent.setAction(Intent.ACTION_VIEW);
	     
	    Uri uri = Uri.fromFile(file);
	     
	    intent.setDataAndType(uri, tipo);
	    
		return intent;
	}
}
